package com.dks.repository.impl.publicCompoment;

import com.dks.businessUtil.QsMailUtil;
import com.dks.dto.publicCompoment.PublicThumbsUpDto;
import com.dks.mapper.CourseMapper;
import com.dks.mapper.PublicThumbsUpMapper;
import com.dks.mapper.UsersMapper;
import com.dks.models.QsMail;
import com.dks.models.Users;
import com.dks.models.publicCompoment.PublicThumbsUp;
import com.dks.repository.IPublicThumbsUpRepository;
import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class PublicThumbsUpRepository implements IPublicThumbsUpRepository {

    @Autowired
    private PublicThumbsUpMapper publicThumbsUpMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 点赞/取消点赞
     */
    @Override
    @Transactional
    public ResultModel<PublicThumbsUp> thumbsUp(PublicThumbsUpDto publicThumbsUpDto) {
        PublicThumbsUp publicThumbsUp = new PublicThumbsUp();
        publicThumbsUp.setThumbsUpEntityType(publicThumbsUpDto.getThumbsUpEntityType().name());
        publicThumbsUp.setThumbsUpEntityId(publicThumbsUpDto.getThumbsUpEntityId());
        publicThumbsUp.setCreateUserId(publicThumbsUpDto.getUserId());
        if (publicThumbsUpDto.getThumbsUpStatus().isJudge()) {
            //点赞
            publicThumbsUpMapper.addthumbsUp(publicThumbsUp);
            if ("TEACHER_DIARY".equals(publicThumbsUpDto.getThumbsUpEntityType())) {
                //日记点赞添加站内信
                Users users = usersMapper.select(publicThumbsUpDto.getUserId());
                String nickName = "";
                if (null != users) {
                    nickName = users.getNickname();
                }
                QsMail qsMail = new QsMail(publicThumbsUpDto.getUserId(), 6, nickName, "点赞了您的作业，快去看看吧", publicThumbsUpDto.getThumbsUpEntityId());
                QsMailUtil qsMailUtil = new QsMailUtil();
                qsMailUtil.insertQsMail(qsMail);
            }
        } else {
            //取消点赞
            publicThumbsUpMapper.removethumbsUp(publicThumbsUp);
        }
        return Result.makeResult(ResultInfo.SUCCESS);
    }
}
